'''
Copyright 2010 Indra Bagus <indra.bagus@gmail.com> 
    
Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

    http://www.apache.org/licenses/LICENSE-2.0 
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. 
'''


class WmxIntegerValue(object):
    def __init__(self,mapinfodict=dict()):
        super(WmxIntegerValue,self).__init__()
        self._map_info = mapinfodict
        self.val = None
        self.strinfo = str()
    
    def SetInfoLookupTable(self,mapinfo ):
        assert(isinstance(mapinfo,dict))
        self._map_info = mapinfo
        

    def __str__(self):
        return self.strinfo
    
    def __int__(self):
        return self.val
        

class WmxStringValue(object):
    def __init__(self):
        self.strval     = str()
        self.strinfo    = str()
    


class WmxDictionaryValue(object):
    '''
    '''
    def __init__(self,dict = {}):
        self._value = 0
        self._str   = ''
        self._dict = dict

        
    @property
    def val(self):
        return self._value

    
    @val.setter
    def val(self,value):
        self._value = value
        try:
            self._str = self._dict[value]
        except KeyError:
            self._str = 'Unknown'            

            
    def __str__(self):
        return self._str
    
    
    def __repr__(self):
        return repr((self._value,self._str))
    
    
    def __int__(self):
        return int(self._value)



class WmxTlvValue(object):
    def __init__(self):
        self._value = str()
        self._value_info = None
        self._nested_tlv_list = list()
    
    def GetValueAsString(self):
        return self._value
    
    
    def SetValueAsString(self,val):
        assert(isinstance(val,str))
        self._value = val
        
    
    def IsNestedTLV(self):
        if(len(self._nested_tlv_list) > 0):
            return True
        else:
            return False
        
    def GetNestedTLVIterator(self):
        return iter(self._nested_tlv_list)
        
    
    def ToInteger(self):
        if len(self._value) > 4 or len(self._value) == 0:
            return None
        
        retval = 0L
        iterbyte = iter(self._value)
        while True:
            try:
                retval = (retval << 8) | ord(iterbyte.next())
            except StopIteration:
                break
            
        return retval
    
    
    
    def SetValueInfo(self,strinfo):
        assert isinstance(strinfo,str)
        self._value_info = strinfo
    
    
    def __int__(self):
        return int(self.ToInteger())
    
    
    def __str__(self):
        return self._value_info



class WmxTLV(object):
    '''
    TLV ( Type Length Value )
    @author: Indra Bagus<indra.bagus@gmail.com>
    '''
    def __init__(self):
        self.Type      = WmxIntegerValue()
        self._length    = int()
        self._value     = WmxTlvValue()
        
        
#    @property
#    def Type(self):
#        return self._type
#    
#    
#    @Type.setter
#    def Type(self,val):
#        assert(isinstance(val,int))
#        self._type.val = val
        
    
    
    @property
    def Length(self):
        return self._length
    
    
    @Length.setter
    def Length(self,val):
        self._length = val
        
    
    @property
    def Value(self):
        return self._value
    
    
    @Value.setter
    def Value(self,val):
        self._value.SetValueAsString(val)
        
    def __repr__(self):
        return repr(self._value)
        
